/*
 * @Author: Clark
 * @Email: haixuanwoTxh@gmail.com
 * @Date: 2023-02-01 12:30:38
 * @LastEditors: Clark
 * @LastEditTime: 2023-02-01 12:30:45
 * @Description: file content
 */
#include "apue.h"

static int pfd1[2], pfd2[2];

void TELL_WAIT(void)
{
    if (pipe(pfd1) < 0 || pipe(pfd2) < 0)
    {
        err_sys("pipe error");
    }
}

void WAIT_PATENT(void)
{
    char c;
    if (read(pfd1[0], &c, 1) != 1)
    {
        err_sys("read error");
    }

    if ('p' != c)
    {
        err_quit("WAIT_PARENT: incorrect data");
    }
}

void TELL_CHILD(pid_t pid)
{
    if (write(pfd1[1], "p", 1) != 1)
    {
        err_sys("write error");
    }
}

void WAIT_CHILD(void)
{
    char c;
    if (read(pfd2[0], &c, 1) != 1)
    {
        err_sys("read error");
    }

    if ('c' != c)
    {
        err_quit("WAIT_CHILD: incorrect data");
    }
}
